package genedit.controller.interfaceActions;

import genedit.controller.DeleteCommand;
import genedit.model.Workspace;
import genedit.model.graph.GraphElement;
import genedit.model.graph.SubdocumentElement;
import genedit.view.graph.Clipboard;
import genedit.view.graph.GraphDocumentView;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 * Akcija zadužena za isecanje selektovanih grafičkih elemenata.
 * 
 * @author Nikola
 * @version 1.0 25/12/13
 */
public class GCutA extends AbstractAction {

	private static final long serialVersionUID = 1L;

	private static GCutA gCutA = null;

	protected GCutA() {
		putValue(NAME, "Cut");
		putValue(SHORT_DESCRIPTION, "Cut");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/cut.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_U);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke("control X"));
	}

	public static AbstractAction getInstance() {
		if (gCutA == null)
			gCutA = new GCutA();
		return gCutA;
	}

	/**
	 * Akcija postavlja selektovane grafičke elemente u klipboard nakon čega
	 * kreira komandu za brisanje i uklanja date elemente iz dijagrama.
	 */
	public void actionPerformed(ActionEvent arg0) {
		GraphDocumentView document = Workspace.getInstance().getActiveDocument();

		if (document == null || document.getModel().getSelectedElements().size() == 0)
			return;

		// zabrana isecanja subdiagrama kao elementa
		List<GraphElement> selectedElements = document.getModel().getSelectedElements();
		for (Iterator<GraphElement> elementIter = selectedElements.iterator(); elementIter
				.hasNext();) {
			GraphElement element = elementIter.next();
			if (element instanceof SubdocumentElement) {
				elementIter.remove();
			}
		}
		if (selectedElements.size() == 0)
			return;

		Clipboard.getInstance().setCopiedElements(selectedElements);
		DeleteCommand dlt = new DeleteCommand(true);
		dlt.setDocument(document);
		dlt.setDeletedElements(selectedElements);

		document.getModel().getInvoker().executeCommand(dlt);
	}

}